package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    DishMapper dishMapper;

    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Override
    public void addDishWithFlavors(DishDTO dishDTO) {
       Dish dish = new Dish();
       BeanUtils.copyProperties(dishDTO,dish);
       dishMapper.insert(dish);
       List<DishFlavor> flavors = dishDTO.getFlavors();
       if (flavors!=null&& !flavors.isEmpty()){
           flavors.forEach(flavor->{
               flavor.setDishId(dish.getId());
           });
           dishFlavorMapper.insert(flavors);
       }



    }

    @Override
    public PageResult getPage(DishPageQueryDTO dto) {
        PageResult result = new PageResult();
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<DishVO> page = dishMapper.getPage(dto);
        result.setTotal(page.getTotal());
        result.setRecords(page.getResult());
        return result;
    }

    @Override
    public void  updateDishWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        dishFlavorMapper.deleteByDishId(dish.getId());
        List<DishFlavor> flavors= dishDTO.getFlavors();
        if (flavors!=null&&!flavors.isEmpty()){
            flavors.forEach(flavor->{
                flavor.setDishId(dish.getId());
            });
            dishFlavorMapper.insert(flavors);
        }
    }

    @Override
    public DishVO getDishById(Long dishId) {
        Dish dish =  dishMapper.selectById(dishId);
        List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(dishId);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    @Override
    public void batchDeleteDish(String ids) {
        String[] split = ids.split(",");
        //  JDK8 Stream API
        Long[] batchId = Arrays.stream(split)
                .map(Long::parseLong)
                .toArray(Long[]::new);

        dishMapper.batchDelete(batchId);
        dishFlavorMapper.batchDelete(batchId);
    }

    @Override
    public List<Dish> getDishByCategory(Long categoryId) {

        return dishMapper.getByCategory(categoryId);
    }

    @Override
    public void startOrStop(int status, Long id) {
        Dish dish = Dish.builder()
                .status(status)
                .id(id)
                .build();
        dishMapper.update(dish);
    }

    @Override
    public List<DishVO> getList(Long categoryId) {
       List<DishVO> dishVOS = new ArrayList<>();

       List<Dish> dishes = dishMapper.getByCategory(categoryId);

       for (Dish dish : dishes) {
           List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(dish.getId());
           DishVO dishVO = new DishVO();
           BeanUtils.copyProperties(dish, dishVO);
           dishVO.setFlavors(flavors);
           dishVOS.add(dishVO);
       }
       return dishVOS;

    }
}
